package com.abe.mvp.view.activity;

import java.util.ArrayList;
import java.util.List;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.abe.mvp.view.activity.base.BaseActivity;
import com.abe.strengthtrainer.R;
import com.abe.utils.NormalUtils;
import com.abe.utils.ThreadPoolUtils;
import com.abe.view.MoveAnimCurveView;
import com.abe.view.MoveAnimCurveView.CurveControl;

@SuppressWarnings("unused")
@ContentView(R.layout.activity_test)
public class TestActivity extends BaseActivity implements CurveControl {

	/**拷贝日志存储的高度集合，把中括号改为花括号*/
	private int[] heightArray = { 11, 10, 5, 3, 2, 2, 4, 7, 17, 30, 34, 40, 44,
			43, 40, 36, 24, 17, 13, 8, 3, 4, 36, 40, 44, 43, 41, 37, 26, 19,
			11, 7, 4, 6, 12, 21, 30, 38, 42, 45, 44, 41, 37, 25, 20, 13, 8, 4,
			4, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 24, 7, 13, 23, 31, 34, 39, 41, 38, 33, 22, 16, 8, 6, 5, 2,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 4, 5, 9, 17, 24, 27, 30, 30, 27, 25, 19, 11, 4, 0, 4, 11, 17,
			24, 28, 29, 28, 24, 21, 13, 10, 2, 3, 8, 12, 22, 25, 29, 30, 29,
			27, 21, 17, 11, 5, 2, 5, 8, 15, 21, 24, 29, 31, 29, 28, 24, 19, 14,
			10, 6, 6, 9, 19, 23, 27, 32, 32, 30, 29, 23, 19, 13, 6, 2, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 5, 10, 14, 24, 30, 35, 40, 39, 36, 30, 24, 17, 14, 10, 4,
			4, 12, 20, 30, 36, 38, 42, 43, 41, 35, 33, 26, 21, 14, 13, 10, 7,
			5, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 10, 17, 22, 30, 40,
			43, 43, 39, 33, 26, 22, 7, 6, 8, 20, 27, 37, 40, 43, 39, 37, 31,
			21, 15, 4, 5, 14, 25, 30, 36, 42, 43, 40, 35, 20, 20, 12, 6, 9, 19,
			27, 35, 38, 43, 43, 40, 38, 31, 29, 20, 16, 9, 7, 9, 13, 20, 20,
			24, 26, 28, 33, 34, 33, 32, 28, 23, 17, 14, 11, 10, 15, 24, 32, 41,
			43, 41, 39, 34, 25, 10, 19, 25, 34, 40, 42, 40, 38, 34, 30, 24, 21,
			15, 12, 10, 9, 9, 7, 6, 5, 4, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	@ViewInject(R.id.macv)
	protected MoveAnimCurveView macv;
	/** 曲线状态 */
	protected boolean isMove = false;
	/** 锻炼器材设置的最大幅度（用来转换小球屏幕位置） */
	protected int customMaxHeight = 40;

	protected List<Integer> heights = new ArrayList<Integer>();
	protected int currentCustomHeight = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/**模拟数据*/
		//initHeightList();
		/**真是锻炼数据*/
		initTrueHeightList();
		ThreadPoolUtils.execute(new delayThread());
	}

	class delayThread implements Runnable {

		@Override
		public void run() {
			try {
				Thread.sleep(1000);
				callStartMove();
				ThreadPoolUtils.execute(new testThread());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	class testThread implements Runnable {

		@Override
		public void run() {
			while (true) {
				for (int i = 0; i < heights.size(); i++) {
					currentCustomHeight = heights.get(i);
					/** 更新小球位置 */
					localCustomSendEmptyMessage(MAIN_REFRESH_BALL_POS);
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	private void initTrueHeightList() {
		for (int i = 0; i < heightArray.length; i++) {
			heights.add(heightArray[i]);
		}
	}

	private void initHeightList() {
		int temHeight = 0;
		for (int i = 0; i < 20; i++) {
			temHeight = 2 + temHeight;
			heights.add(temHeight);
		}
		for (int i = 10; i > 0; i--) {
			temHeight = temHeight - 4;
			heights.add(temHeight);
		}

	}

	@Override
	public void callStartMove() {
		isMove = true;
		localCustomSendEmptyMessage(EXERCISE_START);
	}

	@Override
	public void callRestartMove() {
		isMove = true;
		localCustomSendEmptyMessage(EXERCISE_UPDATE);
	}

	@Override
	public void callStopMove() {
		isMove = false;
		localCustomSendEmptyMessage(EXERCISE_STOP);
	}

	private static final int EXERCISE_START = 0x901;
	private static final int EXERCISE_STOP = 0x902;
	private static final int EXERCISE_UPDATE = 0x903;
	/** 刷新小球位置 */
	protected static final int MAIN_REFRESH_BALL_POS = 0x904;

	protected void localCustomSendEmptyMessage(int what) {
		new CustomHander(Looper.getMainLooper()).sendEmptyMessage(what);
	}

	protected void localCustomSendMessage(int what, Object object) {
		new CustomHander(Looper.getMainLooper()).sendMessage(NormalUtils
				.getMessage(what, object));
	}

	class CustomHander extends Handler {
		public CustomHander(Looper looper) {
			super(looper);
		}

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case EXERCISE_START:
				ThreadPoolUtils.execute(macv);
				break;
			case EXERCISE_STOP:
				macv.stopMove();
				break;
			case EXERCISE_UPDATE:
				macv.restartMove();
				break;
			case MAIN_REFRESH_BALL_POS:
				macv.setBallScreenHeight((float) (customMaxHeight - currentCustomHeight)
						/ (float) customMaxHeight);
				break;
			}
		}
	}
}
